package com.ygsoft.tianlu_mall.controller;

import com.ygsoft.tianlu_mall.pojo.Msg;
import com.ygsoft.tianlu_mall.pojo.vo.req.AddEvaluationReqVO;
import com.ygsoft.tianlu_mall.service.CartService;
import com.ygsoft.tianlu_mall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    OrderService orderService;
    @Autowired
    CartService cartService;

    //在商品详情页点击结算生成订单
    @RequestMapping(value = "/addOneGoodsToOrder",method = RequestMethod.POST)
    @ResponseBody
    public Msg addOneGoodsToOrder(@RequestParam int user_id,@RequestParam int goods_id){
        try {
            orderService.addOneGoodsToOrder(user_id,goods_id);
            int order_id = orderService.getOrderId();
            HashMap<String,Object> result = new HashMap<>();
            result.put("order_id",order_id);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //在购物车点击结算生成订单(按店铺生成订单编号)
    @RequestMapping(value = "/addGoodsToOrderByShopId",method = RequestMethod.POST)
    @ResponseBody
    public Msg addGoodsToOrderByShopId(@RequestParam int user_id, @RequestParam int shop_id,
                                       @RequestParam List<Integer> goods_idList, @RequestParam double total_money){
        try {
            orderService.addGoodsToOrderByShopId(user_id,shop_id,goods_idList,total_money);
            //从购物车删除结算的商品
            for (int goods_id:goods_idList){
                cartService.deleteGoodsFromCart(user_id,(int)goods_id);
            }
            int order_id = orderService.getOrderId();
            HashMap<String,Object> result = new HashMap<>();
            result.put("order_id",order_id);
            result.put("order_createTime",new Date());
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //获得订单信息(结算后跳转的页面信息)
    //根据订单id返回消费者的信息以及订单下所涉及的商品信息
    @RequestMapping(value = "/getOrderInfo",method = RequestMethod.GET)
    @ResponseBody
    public Msg getOrderInfo(@RequestParam int user_id, @RequestParam List<Integer> order_idList){
        try {
            List<HashMap<String,Object>> orderList = new ArrayList<>();
            for (int order_id:order_idList) {
                HashMap<String,Object> orderGoodsInfo = orderService.getOrderGoodsInfo(user_id,order_id);
                orderList.add(orderGoodsInfo);
            }
            HashMap<String,Object> result = new HashMap<>();
            result.put("orderList",orderList);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //为订单付款
    @RequestMapping(value = "/payForOrder",method = RequestMethod.PUT)
    @ResponseBody
    public Msg payForOrder(@RequestParam int user_id, @RequestParam List<Integer> order_idList, @RequestParam double order_totalMoney){
        try {
            //获取用户余额
            BigDecimal user_restMoney = orderService.getUserRestMoney(user_id);
            BigDecimal total_money = BigDecimal.valueOf(order_totalMoney);
            //0表示相等，-1表示小于，1表示大于
            int compareResult = user_restMoney.compareTo(total_money);
            //检查买家余额 不足则提示充值
            if (compareResult >=0 ){
                //买家支付数增加 余额数减少
                orderService.updateUserMoney(user_id,total_money);

                for (int order_id:order_idList) {
                    //更改订单状态 待支付 0 -> 待评价 1
                    HashMap<String,Object> map = new HashMap<>();
                    map.put("order_id",order_id);
                    map.put("order_status",1);
                    orderService.updateOrder2DetailStatus(map);
                    //根据订单id查找不同的卖家
                    int shop_userid = orderService.getShopUserid(order_id);
                    //获得每张订单的金额
                    BigDecimal money = orderService.getEveryOrderMoney(order_id);
                    //卖家收入数增加 余额数增加
                    orderService.updateShopUserMoney(shop_userid,money);
                }
                return Msg.succes();
            }else {
                String result = "余额不足，请先充值再支付！";
                return Msg.fail().add("result",result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //待支付状态付款
    //time为订单创建时间+15分钟
    //订单失效状态码:3
    @RequestMapping(value = "/payForOrderById",method = RequestMethod.PUT)
    @ResponseBody
    public Msg payForOrder(@RequestParam int user_id, @RequestParam int order_id, @RequestParam double order_totalMoney,
                           @RequestParam @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date time){
        //获取用户余额
        BigDecimal user_restMoney = orderService.getUserRestMoney(user_id);
        BigDecimal total_money = BigDecimal.valueOf(order_totalMoney);
        //0表示相等，-1表示小于，1表示大于
        int compareResult = user_restMoney.compareTo(total_money);
        //检查买家余额 不足则提示充值
        if (compareResult >=0 ){
            Date currentTime = new Date();
            int timeCompare = currentTime.compareTo(time);
            //当前时间小于等于订单创建时间15分钟
            if (timeCompare>0){
                //买家支付数增加 余额数减少
                orderService.updateUserMoney(user_id,total_money);
                //更改订单状态 待支付 0 -> 待评价 1
                HashMap<String,Object> map = new HashMap<>();
                map.put("order_id",order_id);
                map.put("order_status",1);
                orderService.updateOrder2DetailStatus(map);
                //根据订单id查找不同的卖家
                int shop_userid = orderService.getShopUserid(order_id);
                //获得每张订单的金额
                BigDecimal money = orderService.getEveryOrderMoney(order_id);
                //卖家收入数增加 余额数增加
                orderService.updateShopUserMoney(shop_userid,money);
                return Msg.succes();
            }else {
                HashMap<String,Object> map = new HashMap<>();
                HashMap<String,Object> closeMap = new HashMap<>();
                map.put("order_id",order_id);
                map.put("order_status",3);
                //更改订单状态 待支付 0 -> 失效 3
                orderService.updateOrder2DetailStatus(map);
                closeMap.put("order_id",order_id);
                closeMap.put("end_time",new Date());
                //设定订单结束时间
                orderService.updateOrderEndTime(closeMap);
                String result = "订单超时失效！";
                return Msg.fail().add("result",result);
            }
        }else {
            String result = "余额不足，请先充值再支付！";
            return Msg.fail().add("result",result);
        }
    }

    //获得商品
    @RequestMapping(value = "/getGoodsFile",method = RequestMethod.GET)
    @ResponseBody
    public Msg getGoodsFile(@RequestParam int user_id, @RequestParam List<Integer> order_idList){
        try {
            HashMap<String,Object> goodsFileMap = orderService.getGoodsFile(user_id,order_idList);
            HashMap<String,Object> result = new HashMap<>();
            result.put("goodsFile",goodsFileMap);
            return Msg.succes().add("result",result);
        }catch (Exception e){
            e.printStackTrace();
            return Msg.fail();
        }

    }


    //我的订单管理
    //“我的订单”展示
    //订单状态order_status含义 0 待支付 1 待评价 2 完成
    @RequestMapping(value = "/getAllOrderInfo",method = RequestMethod.GET)
    @ResponseBody
    public Msg getAllOrderInfo(@RequestParam int user_id){
        try{
            List<HashMap<String,Object>> allOrderInfo = orderService.getAllOrderInfo(user_id);
            HashMap<String,Object> result = new HashMap<>();
            result.put("allOrderInfo",allOrderInfo);
            return Msg.succes().add("result",result);
        }catch (Exception e){e.printStackTrace();
            return Msg.fail();
        }
    }

    //评价
    @RequestMapping(value = "/addEvaluation",method = RequestMethod.POST)
    @ResponseBody
    public Msg addEvaluation(@RequestBody AddEvaluationReqVO vo){
        try {
            orderService.addEvaluation(vo);
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //删除订单
    @RequestMapping(value = "/deleteOrderInfo",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteOrderInfo(int user_id,int order_id){
        try {
            orderService.deleteOrderInfo(user_id,order_id);
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }


}
